package com.ygqh.baby.controller.admin;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.GroupType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgGroup;
import com.ygqh.baby.service.YgGroupService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/group")
public class GroupController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private YgGroupService ygGroupService;

	@RequestMapping("searchGroup")
	@ResponseBody
	public Map<String, Object> searchGroup(String q, QueryInfo queryInfo, GroupType groupType, Boolean isGroup) {

		if (queryInfo == null) {
			queryInfo = new QueryInfo();
			queryInfo.setStart(1);
			queryInfo.setLimit(100);
		}
		ResultSet<YgGroup> resultSet = ygGroupService.search(queryInfo, getSupplier().getId(), q, groupType, isGroup);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("searchGroupList")
	@ResponseBody
	public List<YgGroup> searchGroupList(GroupType groupType) {
		if (groupType == null)
			groupType = GroupType.Web;

		return ygGroupService.find(groupType,getSupplier().getId());

	}

	@RequestMapping("saveGroup")
	@ResponseBody
	public Message saveGroup(String strGroup) {
		YgGroup group = JsonUtils.toObject(strGroup, YgGroup.class);

		group.setSupplierId(getSupplier().getId());
		group.setCreateBy(getSession().getUserName());
		group.setCreateTime(new Date());
		// 判断是否含有相同的名字
		if (checkName(group.getGroupName(), group.getId(), group.getGroupType()) > 0)
			return Message.error("分类名称已经存在");
		if (ygGroupService.saveOrUpdate(group) == 1) {
			return Message.success(SUCCESS_MESSAGE.getCode(), group.getId());
		}
		return ERROR_MESSAGE;
	}

	@RequestMapping("updateGroupStatus")
	@ResponseBody
	public Message updateGroupStatus(Long id) {
		YgGroup ygGroup = new YgGroup();
		ygGroup.setId(id);
		ygGroup.setUpdateBy(getSession().getUserName());
		ygGroup.setUpdateTime(new Date());
		ygGroup.setStatus(DataStatus.Delete);
		return ygGroupService.update(ygGroup) == 1 ? SUCCESS_MESSAGE : ERROR_MESSAGE;
	}

	private int checkName(String name, Long id, GroupType groupType) {
		return ygGroupService.findName(name, id, groupType);
	}

	@RequestMapping("updateGroup")
	@ResponseBody
	public Message updateGroup(Long id, BigDecimal promotionPrice, Long groupAmount, BigDecimal shareRate) {
		YgGroup group = new YgGroup();
		group.setUpdateBy(getSession().getUserName());
		group.setUpdateTime(new Date());
		group.setId(id);
		group.setGroupAmount(groupAmount);
		group.setPromotionPrice(promotionPrice);
		group.setShareRate(shareRate);
		ygGroupService.updatePromotion(group);
		return SUCCESS_MESSAGE;
	}

}
